package com.mijie.query;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

import com.mijie.fentsi.fentsiOr;
import com.mijie.fentsi.renvaEventC;
import com.mijie.homi.search.util.Dbcontroller;
import com.mijie.homi.search.util.god;
import com.mijie.homi.search.util.timeTester;
import com.mijie.query.fulltxtCtrlMultiThrd.sql_queue_thread;


/**
 * zafei 
 * @author Administrator
 *
 */
public class fulltxtIndexorCbc extends fulltxtCtrlMultiThrd {

	
	public static void main(String[] args) {
		 
		try {
	 //	 Thread.sleep(15000);
			 
		} catch (Exception e) {
			 
			e.printStackTrace();
		}
		
		timeTester t = new timeTester(" cb1");
		fulltxtIndexorCbc c = new fulltxtIndexorCbc();
		fulltxtCtrlMultiThrd.mood4fullTable = "mood4fullTest";
		c.indexBatchID =god.getUUid();
		c.indexAll();
		t.printUseTime();
		
		
	}
	
	public void indexAll() {

		// ////ini
		if (indexBatchID == null)
			throw new RuntimeException("indexBatchID is null");

		dbc = new Dbcontroller();
		dbc.setAutoCommit(false);

		// ExecutorService executor = Executors.newSingleThreadExecutor();
		// 如果不需要特定的结果，则考虑使用下列形式的构造：Future<?> f = new
		// FutureTask<Object>(runnable, null)
		FutureTask futureTask = new FutureTask(new Callable() {// 使用Callable接口作为构造参数
					public Object call() {

						// 真正的任务在这里执行，这里的返回值类型为String，可以为任意类型

						// ini wordLib need2s
						fentsiOr fentsiCca22 = new fentsiOr();
						fentsiCca22.iniFentsiWordlibFromdb();// haosi 2s
						return fentsiCca22;
					}
				});
		// 使用futureTask创建一个线程
		Thread Thread1 = new Thread(futureTask);
		Thread1.setName(" thread 4 load  FentsiWordlib");
		Thread1.setPriority(Thread.MAX_PRIORITY);
		Thread1.start();
		// 测试后台的计算线程是否完成，如果未完成则等待
		// if (!futureTask.isDone()) {
		
		
		//cbc
		FutureTask futureTask_loadRevaEventController = new FutureTask(new Callable() {// 使用Callable接口作为构造参数
			public Object call() {
				logger.info("--start ini renva envent db");
				renvaEventC rec = new renvaEventC();
				rec.iniTypeKeywordLibFromDB();
				logger.info("  rec.iniTypeKeywordLibFromDB(); ok");
				rec.iniEventKeywordlibFromdb();
				
				
				// ini wordlib in writeToIndex();
				// ini word_stopword not need
				// cb3 ini map_Renva4problem
				inimap4Renva(); // prob renva fenlei map 180ms
				return rec;
			}
		});
		Thread Thread_loadRevaNevent = new Thread(futureTask_loadRevaEventController);
		Thread_loadRevaNevent.setName(" Thread_loadRevaNevent");
		Thread_loadRevaNevent.setPriority(Thread.MIN_PRIORITY);
		Thread_loadRevaNevent.start();

		System.out.println("--other work");
		// need 300 ms about
		
		

		// renvaEventC rec = null;
		//System.out.println("--start ini renva envent db finish");
	
		//System.out.println(fentsiOrcb9);

		// /start to sqlqueue
		// dbc4sql_queue_thread = new Dbcontroller();
		Thread tdcba = new Thread(new sql_queue_thread());
		tdcba.setPriority(Thread.MAX_PRIORITY);
		tdcba.setName("--thd sqlqueue processor ");
	 //	tdcba.start();

		// if(true)
		// return ;
		// executor.execute(future);
		// 在这里可以做别的任何事情
	

		// ba30 only sincin lib
		System.out.println(" c2a");
		List li = this.readDBx(10000);
		this.lidb = li;
		this.from = "sincinlib";
		System.out.println(" c2b");
		
		
		
		//cbc  get rec and fentsicyi
		fentsiOr fentsiOrcb9=(fentsiOr) god.getThreadResult(futureTask);
		this.renvaEventCca29 = (renvaEventC) god.getThreadResult (futureTask_loadRevaEventController);	
		this.fentsiCca29 = fentsiOrcb9;
		
		int cpu=2;
//		int 
//		List li_part1=li.subList(0, 5000);
//		List li_part2
		writeToIndex();
		 
		System.out.println(" c2");
		

		// cb3 process problemlib
		List li_pro = readDB4pro(100000);
		this.lidb = li_pro;
		this.from = "problemlib";
		// writeToIndex();
		
		
		
		

		System.out.println("---f");
		es.shutdown();
		System.out.println("---fca29");

		try {
			es.awaitTermination(3, TimeUnit.HOURS);
			threadPoosIsComplete = true;
			logger.info("es.awaitTermination  finish");
		} catch (InterruptedException e) {

			e.printStackTrace();
			logger.info("es.awaitTermination err");
		}

		// db9 change to theread syaogo haosyeo bminsye
		// Thread td= new Thread(new Runnable() {
		// public void run() {
		//
		// dbc.commit();
		// System.out.println(Thread.currentThread().getName());
		//
		// }
		// });
		// td.setPriority(Thread.MAX_PRIORITY);
		// td.setName("--thd cb9 finally");
		// td.start();
		// LastDBC.commit();
		// LastDBC.close();

		// if(Sqlqueue.size()==0)
		// tdcba
		// dbc4sql_queue_thread.close();
		logger.info("--  commit db  last : Sqlqueue.size " + Sqlqueue.size());
		// dbc4sql_queue_thread.commit();
		logger.info(" commit last db");
		try {
			tdcba.join();
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
		System.out.println("---finish");

	}
}
